package com.hxkj.controller;

import com.hxkj.client.service.CertExpertServiceClient;
import com.hxkj.client.service.FieldTagServiceClient;
import com.hxkj.domain.enums.UserExceptionEnum;
import com.hxkj.domain.po.CertExpert;
import com.hxkj.domain.po.FieldTag;
import com.hxkj.domain.po.Token;
import com.hxkj.service.TokenService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * created by kezhijie on 2018/10/9 16:05.
 * email to kezhijie@hxoss.com
 */
@RestController
@RequestMapping("/expert")
public class CertExpertController {

    @Autowired
    private CertExpertServiceClient certExpertServiceClient;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private FieldTagServiceClient fieldTagServiceClient;

    @PostMapping("save")
    public void save(@ModelAttribute CertExpert certExpert, HttpServletRequest request) throws Exception {
        String userId = getUserIdByToken(request);

        certExpert.setUserId(userId);

        ResultMessageStruct resultMessageStruct = certExpertServiceClient.save(certExpert);

        // 处理反馈消息
        resolveMessage(resultMessageStruct);
    }

    @PostMapping("update")
    public void update(@ModelAttribute CertExpert certExpert, HttpServletRequest request) throws Exception {
        String userId = getUserIdByToken(request);

        certExpert.setUserId(userId);

        ResultMessageStruct resultMessageStruct = certExpertServiceClient.update(certExpert);

        // 处理反馈消息
        resolveMessage(resultMessageStruct);

    }

    @PostMapping("get")
    public Map<String, Object> get(HttpServletRequest request) throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        String userId = getUserIdByToken(request);

        ResultDataStruct<CertExpert> resultDataStruct = certExpertServiceClient.getByUserId(userId);

        if(resultDataStruct.getCode() != 200){
            throw new CustomMessageException(resultDataStruct.getMessage());
        }
        Map<String, Object> params = new HashMap<>();
        params.put("refId",resultDataStruct.getData() == null ? null : resultDataStruct.getData().getId());
        params.put("classify","field");
        ResultDataStruct<List<FieldTag>> fieldResult = fieldTagServiceClient.selectCertFieldTag(params);
        if(fieldResult.getCode() != 200){
            throw new CustomMessageException(fieldResult.getMessage());
        }
        resMap.put("fieldList", fieldResult.getData());
        params.put("classify","tag");
        ResultDataStruct<List<FieldTag>> tagResult = fieldTagServiceClient.selectCertFieldTag(params);
        if(tagResult.getCode() != 200){
            throw new CustomMessageException(tagResult.getMessage());
        }
        resMap.put("tagList",tagResult.getData());

        resMap.put("certExpert",resultDataStruct.getData() == null ? new CertExpert() : resultDataStruct.getData());

        return resMap;
        // 处理反馈消息
        //return resolveDataMessage(resultDataStruct);
    }

    private String getUserIdByToken(HttpServletRequest request) {
        String accessToken = request.getHeader("accessToken");
        Token token = tokenService.validate(accessToken).getData();

        return token.getUserId();
    }

    private void resolveMessage(ResultMessageStruct resultMessageStruct) throws Exception {
        if (resultMessageStruct.getCode() == 500)
            throw new CustomMessageException(resultMessageStruct.getMessage());
        if (resultMessageStruct.getCode() != 200)
            throw new CustomException(UserExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
    }

    private <T> T resolveDataMessage(ResultDataStruct<T> resultDataStruct) throws Exception {
        if (resultDataStruct.getCode() == 500)
            throw new CustomMessageException(resultDataStruct.getMessage());
        if (resultDataStruct.getCode() != 200)
            throw new CustomException(UserExceptionEnum.getEnumByCode(resultDataStruct.getCode()));
        return resultDataStruct.getData();
    }
}
